package dk.aau.walkietalkie.audio;

import java.io.File;
import java.io.IOException;

import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;

//public class VoiceRecorder {
//	
//	private static final String LOG_TAG = "AudioRecorderExample";
//	private MediaRecorder mRecorder;
//	private String mFileName = Environment.getExternalStorageDirectory().getAbsolutePath()+"/audiorecordtest.3gp";
//
//	public void startRecording() {
//		try {
//			mRecorder = new MediaRecorder();
//			mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//			mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//			File outputFile = new File(mFileName);
//			if (outputFile.exists())
//				outputFile.delete();
//			outputFile.createNewFile();
//			mRecorder.setOutputFile(mFileName);
//			mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//			// mRecorder.setMaxDuration(max_duration_ms);
//			// mRecorder.setMaxFileSize(...);
//			mRecorder.prepare();
//			mRecorder.start();
//		} catch (IOException e) {
//			Log.e(LOG_TAG, "prepare() failed");
//		}
//	}
//
//	public void stopRecording() {
//		mRecorder.stop();
//		mRecorder.release();
//		mRecorder = null;
//	}
//}
public class VoiceRecorder {
	private static final String LOG_TAG = "VoiceRecorder";
	private MediaRecorder mRecorder = null;
	private String VOICE_PATH = Environment.getExternalStorageDirectory()
			.getAbsolutePath() + "/audiorecordtest.3gp";

	// private static final String VOICE_PATH = Environment
	// .getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)
	// .getPath()
	// + "/tmp_audio.jpg";

	public void startRecording(String filePath) {
		if ("".equals(filePath)) {
			filePath = VOICE_PATH;
		}
		try {
			if (mRecorder != null) {
				mRecorder.reset();
			}
			mRecorder = new MediaRecorder();
			mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
			mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
			File outputFile = new File(filePath);
			if (outputFile.exists())
				outputFile.delete();
			outputFile.createNewFile();
			mRecorder.setOutputFile(filePath);
			mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
			// mRecorder.setMaxDuration(max_duration_ms);
			// mRecorder.setMaxFileSize(...);
			mRecorder.prepare();
			mRecorder.start();
		} catch (IOException e) {
			Log.e(LOG_TAG, "prepare() failed");
		}
	}

	public void stopRecording() {
		mRecorder.stop();
		mRecorder.release();
		mRecorder = null;
	}
}
